// leftView.js
import { WebContentsView } from 'electron'
import createContextMenu from '../components/contextMenu';
import handleDownload from '../components/download'
import handleRequest from '../components/getData'
import path from 'node:path'

const url = 'https://www.zaih.com/'
// const preloadPath = path.join(__dirname, '../preload/other.js');

const createLeftView = (win, config) => {
  const leftView = new WebContentsView({
    webPreferences: {
      nodeIntegration: true,
      contextIsolation: true,
      // preload: preloadPath,
      sandbox: false
    },
  });

  // 监听右键菜单事件
  leftView.webContents.on('context-menu', (e, params) => {
    const menu = createContextMenu(leftView);
    menu.popup({ window: win });
  });

  leftView.webContents.openDevTools();

  // 监听下载
  handleDownload(leftView);

  // 监听请求
  handleRequest(leftView);

  leftView.webContents.loadURL(url);
  leftView.setBounds({ x: 0, y: 0, width: config.mainWidth, height: config.mainHeight });
  win.contentView.addChildView(leftView);


  return leftView;
};

export default createLeftView;